package cn.djun.news.controller;

import cn.djun.news.entity.Collect;
import cn.djun.news.service.CollectService;
import cn.djun.news.util.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;


/**
 * @author djun
 * @classname CollectController
 * @description
 * @date 2020/12/3 9:20
 * @email 3170495286@qq.com
 */
@Slf4j
@RestController
@RequestMapping("/collect")
@Api(tags = "新闻收藏API")
public class CollectController {

    @Autowired
    private CollectService collectService;

    /**
     * 收藏新闻（添加）
     *
     * @param collect
     * @return
     */
    @PutMapping("/add")
    @ApiOperation(value = "收藏新闻", notes = "收藏新闻（添加）", httpMethod = "PUT")
    public ResponseEntity<BaseResponse> addCollect(@RequestBody Collect collect) {
        try {
            //调用service层方法
            int flag = collectService.addCollect(collect);
            if (flag == 1) {
                return BaseResponse.generateBadResponseEntity(Constant.SUCCES_CODE, Constant.COLLECT_SUCCES, "");
            } else {
                return BaseResponse.generateBadResponseEntity(Constant.SERVE_ERROR, Constant.COLLECT_ERROR, "");
            }
        } catch (Exception e) {
            log.error("CollectController-->>addCollect", e);
            return BaseResponse.generateBadResponseEntity(Constant.SERVE_ERROR, Constant.SERVE_ERROR_MSG, "");
        }
    }

    /**
     * 分页(查询)
     *
     * @param collect
     * @return
     */
    @GetMapping("/list")
    @PassToken
    @ApiOperation(value = "查看用户收藏", notes = "根据newsid或者userid查看收藏列表", httpMethod = "GET")
    public ResponseEntity<BaseResponse<Paginating<Collect>>> getCollectList(Collect collect) {
        log.debug(collect.toString());
        try {
            Paginating<Collect> paginating = collectService.getCollectlist(collect);
            return BaseResponse.generateBadResponseEntity(Constant.SUCCES_CODE, Constant.SELECT_SUCCES, paginating);
        } catch (Exception e) {
            log.error("UserinfoController-->>getUserList" + e);
            return BaseResponse.generateBadResponseEntity(Constant.SERVE_ERROR, Constant.SERVE_ERROR_MSG, "");
        }
    }

    /**
     * 根据id取消收藏（删除）
     *
     * @param request
     * @param collect
     * @return
     */
    @DeleteMapping("/del")
    @ApiOperation(value = "取消收藏", notes = "根据id取消收藏（删除）", httpMethod = "DELETE")
    public ResponseEntity<BaseResponse> delCollect(HttpServletRequest request, @RequestBody Collect collect) {
        try {
            int flag = collectService.delCollect(collect);
            if (flag == 1) {
                return BaseResponse.generateBadResponseEntity(200, Constant.COLLECT_CANCEL_SUCCES, "");
            } else {
                return BaseResponse.generateBadResponseEntity(500, Constant.COLLECT_CANCEL_FAILD, "");
            }
        } catch (Exception e) {
            log.error("UserinfoController-->>delUser", e);
            return BaseResponse.generateBadResponseEntity(Constant.SERVE_ERROR, Constant.SERVE_ERROR_MSG, "");
        }
    }
}
